<?php

namespace addons\shopro\service\user;

use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\user\User as UserModel;

class User
{
    /**
     * @name 追加消费金额
     * @param  int|object  $user  会员对象或会员ID
     * @param  float  $amount  变更金额
     * @return boolean
     */
    public static function consume($user, $amount)
    {
        // 判断金额
        if ($amount == 0) {
            return false;
        }

        // 判断用户
        if (is_numeric($user)) {
            $user = UserModel::getById($user);
        }
        if (!$user) {
            error_stop('未找到用户');
        }
        $userInfo = $user;
        $childrenModel = new Children();
        $member_id_arr = $childrenModel->where(['children_id'=>$user->id])->column('member_id');
        if($member_id_arr){
            $userModel = new \app\common\model\User;
            $userData = $userModel->whereIn('id',$member_id_arr)->select();
            if($userData){
                $accelerationModel    = new Acceleration();
                foreach ($userData as $k=>$user){
                    $performance          = $user->team_buy_money + $amount;
                    $where['id']          = ['neq', $user->level];
                    $where['performance'] = ['elt', $performance];
                    $where['status']      = 1;
                    $accelerationInfo     = $accelerationModel->where($where)->order('id desc')->find();
                    if ($accelerationInfo) {
                        $is_level = false;
                        if($user->level < $accelerationInfo->id){
                            if(!$accelerationInfo->extend && !$accelerationInfo->push_num){
                                $is_level = true;
                            }else{
                                if ($accelerationInfo->push_num > 0) {
                                    $push_num = UserModel::where(['parent_user_id' => $user->id])->count();
                                    if ($push_num >= $accelerationInfo->push_num) {
                                        $is_level = true;
                                    }
                                }
                                if($accelerationInfo->extend > 0){
                                    $extend_num =  UserModel::where(['parent_user_id' => $user->id,'level'=>$user->level])->count();
                                    if($extend_num >= $accelerationInfo->extend){
                                        $is_level = true;
                                    }
                                }
                            }
                        }
                        if($is_level){
                            $user->level = $accelerationInfo->id;
                        }
                    }
//                    if($user->is_old == 1){
//                        $user->is_old = 0;
//                    }
                    $user->team_buy_money = $performance;
                    $user->save();
                }
            }
        }
        // 更新会员余额信息
        $userInfo->setInc('total_consume', $amount);
        //判断是否能升级
        return true;
    }
}
